package com.edu.chapter07;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.zip.InflaterInputStream;

public class PropertyAnimActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property_anim);
        TextView textView = findViewById(R.id.tv_property);
//        textView.setPivotX(0f);
        textView.setPivotY(0f);
        @SuppressLint("ResourceType") ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.drawable.anim_property);
        animator.setTarget(textView);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.start();


        ImageView imageView = findViewById(R.id.iv_property);
//        imageView.setPivotX(0f);
//        imageView.setPivotY(0f);

        ObjectAnimator rotationY = ObjectAnimator.ofFloat(imageView, View.ROTATION_Y, 0f, 360f);
//        rotationY.setDuration(2000);
        rotationY.setRepeatCount(ValueAnimator.INFINITE);
//        rotationY.start();

        ObjectAnimator translationY = ObjectAnimator.ofFloat(imageView, View.TRANSLATION_Y, 0f, 400f);
        translationY.setRepeatCount(ValueAnimator.INFINITE);

        ObjectAnimator translationX = ObjectAnimator.ofFloat(imageView, View.TRANSLATION_X, 0f, 200f);
        translationX.setRepeatCount(ValueAnimator.INFINITE);

        ObjectAnimator rotateX = ObjectAnimator.ofFloat(imageView, View.ROTATION_X, 0f, 360f);
        rotateX.setRepeatCount(ValueAnimator.INFINITE);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(rotationY, translationY, translationX, rotateX);
        animatorSet.setDuration(2000);
        animatorSet.start();

        TextView tv_2 = findViewById(R.id.tv_property2);
        // ValueAnimator.ofFloat(0f, 1f) 定义了一个从 0 线性变化到 1 的浮点值动画。此值通过监听器驱动视图属性变化
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                float animatedValue = (float) animation.getAnimatedValue();
                tv_2.setRotationX(animatedValue * 360f);
                tv_2.setTranslationX(animatedValue * 1000);
            }
        });
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
//        valueAnimator.setInterpolator(new AccelerateInterpolator(0.2f));
        valueAnimator.setInterpolator(new OvershootInterpolator(2.5f));
        valueAnimator.setDuration(3000);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.start();

        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(tv_2, "backgroundColor", Color.RED, Color.BLUE);

        objectAnimator.setEvaluator(new ArgbEvaluator());

//        objectAnimator.setInterpolator(new OvershootInterpolator(2.5f));
        objectAnimator.setDuration(6000);
        objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
        objectAnimator.start();


        TextView tv_3 = findViewById(R.id.tv_property3);
        tv_3.animate()
                .rotationX(360)
                .translationX(1000)
                .alpha(0)
                .setDuration(2000)
                .start();

    }
}